import { calcFaceArea, calcFaceNormal } from "../utils";
import { VEHeader, VEType, VELoop, VEVertex } from "./";

export class VEFace {

  /**
   * 通用状态记录
   */
  public header: VEHeader;

  public index: number;
  private static _counter = -1;

  /**
   * UV分组flag，可无视
   */
  public uv_group: number = -1;

  /**
   * 当前面片所包含的第一个loop
   */
  public first_loop: VELoop;

  public len: number = 3;

  public normal: BABYLON.Vector3 = BABYLON.Vector3.Zero();

  public constructor(loops: VELoop[]) {
    this.header = new VEHeader(VEType.Face);
    this.first_loop = loops[0];
    this.len = loops.length;

    // this.area = calcFaceArea(loops[0].vertex.position, loops[1].vertex.position, loops[2].vertex.position);

    this.normal = calcFaceNormal(loops[0].vertex.position, loops[1].vertex.position, loops[2].vertex.position);

    this.index = ++VEFace._counter;
  }

  /**
   * face序号记录reset
   */
  public static start(): void {
    VEFace._counter = -1;
  }


  public hasVertex(vertex: VEVertex) {
    if (this.first_loop) {
      for (const he of this.first_loop.nextLoop()) {
        if (he.vertex === vertex) {
          return true;
        }
      }
    }
    return false;
  }
}